<?php

namespace App\Remote\Feishu\Departments;

use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Http;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpFoundation\Request as RequestAlias;

class DepartmentsRemote extends Remote
{

    /**
     * @param string|null $departmentId
     * @param string $tenantAccessToken
     * @param bool $fetchChild
     * @param string $pageToken
     * @return mixed
     * @throws GuzzleException
     * @throws ValidationException
     */
    public static function departments(
        ?string $departmentId,
        string $tenantAccessToken,
        bool $fetchChild = false,
        string $pageToken = ""
    ) {
        return self::customRequestV3(
            "/open-apis/contact/v3/departments?" . http_build_query([
                "department_id_type"   => "open_department_id",
                "parent_department_id" => $departmentId ?? 0,
                "fetch_child"          => $fetchChild,
                "page_size"            => 50,
                "page_token"           => $pageToken
            ]),
            RequestAlias::METHOD_GET,
            null,
            ["Authorization" => sprintf("Bearer %s", $tenantAccessToken)]
        );
    }

    /**
     * @param string $departmentId
     * @param string $tenantAccessToken
     * @return mixed
     * @throws GuzzleException
     * @throws ValidationException
     */
    public static function parentDepartments(string $departmentId, string $tenantAccessToken)
    {
        return self::customRequestV3(
            "/open-apis/contact/v3/departments/parent?" . http_build_query([
                "department_id_type" => "open_department_id",
                "department_id"      => $departmentId,
                "page_size"          => 50,
            ]),
            RequestAlias::METHOD_GET,
            null,
            ["Authorization" => sprintf("Bearer %s", $tenantAccessToken)]
        );
    }

    /**
     * @param string $departmentId
     * @param string $tenantAccessToken
     * @return mixed
     * @throws GuzzleException
     * @throws ValidationException
     */
    public static function departmentInfo(string $departmentId, string $tenantAccessToken)
    {
        return self::customRequestV3(
            "/open-apis/contact/v3/departments/{$departmentId}",
            RequestAlias::METHOD_GET,
            [
                "department_id_type" => "open_department_id",
                "department_id"      => $departmentId,
            ],
            ["Authorization" => sprintf("Bearer %s", $tenantAccessToken)]
        );
    }


    /**
     * 通过审批实例code获取详情
     * @throws GuzzleException
     * @throws ValidationException
     * @doc https://open.feishu.cn/document/server-docs/approval-v4/instance/get
     */
    public static function getInstanceInfo(string $instanceId,string $tenantAccessToken)
    {
        return self::customRequestV3(
            "/open-apis/approval/v4/instances/{$instanceId}",
            RequestAlias::METHOD_GET,
            null,
            ["Authorization" => sprintf("Bearer %s", $tenantAccessToken)]
        );
    }

}
